package com.enterprise.support.utility.generator.panel;

import com.enterprise.application.swing.jeta.AbstractPanel;
import com.enterprise.support.utility.Enterprise;
import com.enterprise.support.utility.SwingUtility;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-1-31
 * Time: 15:26:54
 * To change this template use File | Settings | File Templates.
 */
public class TabProject extends AbstractPanel {
    private final static File srcDirectory = new File(com.enterprise.support.utility.Configuration.LibDirectory, "/java/src");
    private final static File comDirectory = new File(srcDirectory, "com");
    //srcDirectory
    private final static DefaultTableModel projectModel = new DefaultTableModel(new Vector() {{
        add("项目名称");
    }}, 0);
    private final static DefaultTableModel moduleModel = new DefaultTableModel(new Vector() {{
        add("模块名称");
    }}, 0);

    public TabProject() {
        super("com\\enterprise\\support\\utility\\generator\\panel\\TabProject.xml");
    }

    public void initComponents() {
        projectModel.getDataVector().clear();
        super.mainPanel.getTable("projects").setModel(projectModel);
        super.mainPanel.getTable("modules").setModel(moduleModel);
        File[] files = comDirectory.listFiles();
        if (null != files) {
            for (final File directory : files) {
                if (directory.isDirectory()) {
                    projectModel.addRow(new Vector() {{
                        add(directory.getName());
                    }});
                }
            }
        }
    }

    public void addListeners() {

        super.mainPanel.getButton("addproject").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                final Enterprise input = getInput();
                if (input.isEmpty("projectname")) {
                    SwingUtility.warning("项目名为必填！");
                } else if (!input.getString("projectname").matches("^[a-zA-Z].*")) {
                    SwingUtility.warning("项目名必须以字母开头！");
                } else {
                    for (Object values : projectModel.getDataVector()) {
                        if (input.equals("projectname", ((Vector) values).get(0))) {
                            SwingUtility.warning(input.getString("projectname") + " 项目已存在！");
                            return;
                        }
                    }
                    Vector value = new Vector();
                    value.add(input.getString("projectname").trim());
                    projectModel.addRow(value);
                    new File(comDirectory, input.getString("projectname").trim()).mkdirs();
                    mainPanel.getTextField("projectname").setText("");
                }
            }
        });
        super.mainPanel.getTable("projects").addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent ex) {
                String filepath = String.valueOf(projectModel.getValueAt(mainPanel.getTable("projects").getSelectedRow(), 0));
                File projectDirectory = new File(new File(comDirectory, filepath), "application");
                File[] files = projectDirectory.listFiles();
                if (files != null) {
                    moduleModel.getDataVector().clear();
                    for (final File directory : files) {
                        if (directory.isDirectory()) {
                            moduleModel.addRow(new Vector() {{
                                add(directory.getName());
                            }});
                        }
                    }
                } else {
                    moduleModel.getDataVector().clear();
                    moduleModel.addRow(new Object[]{});
                }
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });

        super.mainPanel.getButton("deleteproject").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Integer index = mainPanel.getTable("projects").getSelectedRow();
                if (index > -1) {
                    String filepath = String.valueOf(projectModel.getValueAt(mainPanel.getTable("projects").getSelectedRow(), 0));
                    if (SwingUtility.confirm("该操作无法恢复，请确定？") == JOptionPane.YES_OPTION && !filepath.equalsIgnoreCase("enterprise")) {
                        File temp = new File(comDirectory, filepath);
                        if (temp.delete()) {
                            projectModel.removeRow(index);
                        } else {
                            SwingUtility.warning(temp.getAbsolutePath() + "无法删除,被程序占用!");
                        }
                    }
                } else {
                    SwingUtility.warning("请选择要删除的记录!");
                }
            }
        });


        super.mainPanel.getButton("addmodule").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Integer index = mainPanel.getTable("projects").getSelectedRow();
                final Enterprise input = getInput();
                if (!(index > -1)) {
                    SwingUtility.warning("请选择需要新增模块的项目！");
                } else if (input.isEmpty("modulename")) {
                    SwingUtility.warning("模块名为必填！");
                } else if (!input.getString("modulename").matches("^[a-zA-Z].*")) {
                    SwingUtility.warning("模块名必须以字母开头！");
                } else {
                    for (Object values : moduleModel.getDataVector()) {
                        if (input.equals("modulename", ((Vector) values).get(0))) {
                            SwingUtility.warning(input.getString("modulename") + " 模块已存在！");
                            return;
                        }
                    }
                    Vector value = new Vector();
                    value.add(input.getString("modulename").trim());
                    moduleModel.addRow(value);
                    String filepath = String.valueOf(projectModel.getValueAt(mainPanel.getTable("projects").getSelectedRow(), 0));
                    File project = new File(new File(new File(comDirectory, filepath), "application"), input.getString("modulename").trim());
                    new File(project, "web").mkdirs();
                    new File(new File(project, "logic"), "impl").mkdirs();
                    new File(project, "domain").mkdirs();
                    mainPanel.getTextField("modulename").setText("");
                }
            }
        });
        super.mainPanel.getButton("deletemodule").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Integer index = mainPanel.getTable("modules").getSelectedRow();
                if (index > -1) {
                    String filepath = String.valueOf(projectModel.getValueAt(mainPanel.getTable("projects").getSelectedRow(), 0));
                    if (SwingUtility.confirm("该操作无法恢复，请确定？") == JOptionPane.YES_OPTION) {
                        System.out.println(new File(new File(comDirectory, "application"), filepath).getAbsolutePath());

                        File del = new File(new File(new File(comDirectory, "application"), filepath), moduleModel.getValueAt(index, 0).toString());
                        if (!del.delete()) {
                            System.out.println(del);
                        }
                        moduleModel.removeRow(index);
                    }

                } else {
                    SwingUtility.warning("请选择要删除的记录!");
                }
            }
        });
    }
}
